<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:processor name="oxf:request">
		<p:input name="config">
			<config >
				<include>/request/parameters</include>
			</config>
		</p:input>
		<p:output name="data" id="request"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="#request#xpointer(/request/parameters)"/>
		<p:output name="data" id="parameters"/>
		<p:input name="config">
			<xsl:stylesheet version="2.0">
				<xsl:template match="/">
					<parameters>
						<xsl:apply-templates/>
					</parameters>
				</xsl:template>
				<xsl:template match="parameter">
					<xsl:element name="{./name}">
						<xsl:value-of select="value"/>
					</xsl:element>
				</xsl:template>
			</xsl:stylesheet>
		</p:input>
	</p:processor>

	<p:choose href="#parameters">
		<p:when test="/parameters/widget">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#parameters"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:query collection="/db/portal" xsl:version="2.0">
						<xsl:text>xquery version "1.0";</xsl:text>
						<object>
							<widget>
								<xsl:value-of select="/parameters/widget"/>
							</widget>
							<tags>{" "}{for $d in /widgetsList/category/widget[@id = '<xsl:value-of select="/parameters/widget"/>']/tags/tag return concat($d/text(), ' ')}</tags>
						</object>
					</xdb:query>
				</p:input>
			</p:processor>
		</p:when>
		<p:when test="/parameters/q">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#parameters"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:query collection="/db/portal" xsl:version="2.0">
						<xsl:text>xquery version "1.0";</xsl:text>
						<string>{for $d in /widgetsList/category/widget/tags/tag/text() return if ($d = upper-case('<xsl:value-of select="/parameters/q"/>')) then $d else ""}</string>
					</xdb:query>
				</p:input>
			</p:processor>
		</p:when>
		<p:when test="/parameters/cloud">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#parameters"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:query collection="/db/portal" xsl:version="2.0">
						<xsl:text>xquery version "1.0";</xsl:text>
						<object>{for $d in distinct-values(/widgetsList/category/widget/tags/tag/text()) order by count(/widgetsList/category/widget/tags/tag/text()[. = $d]) return element {$d}{ /widgetsList/category/widget/tags/tag[text() = $d]/last()}}</object>
					</xdb:query>
				</p:input>
			</p:processor>
		</p:when>
	</p:choose>

	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
		<p:output name="data" id="response"/>
	</p:processor>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="utils/json.xpl"/>
		<p:input name="data" href="#response"/>
	</p:processor>

</p:config>